package com.qdlc.p2p.manage.action.additional;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.opensymphony.xwork2.ModelDriven;
import com.qdlc.p2p.biz.service.ExperienceRuleService;
import com.qdlc.p2p.biz.service.ExperienceService;
import com.qdlc.p2p.biz.util.excel.ExcelUtils;
import com.qdlc.p2p.biz.util.excel.JsGridReportBase;
import com.qdlc.p2p.biz.util.excel.TableData;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.enums.EnumActicityPlanNid;
import com.qdlc.p2p.common.util.MessageUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.Experience;
import com.qdlc.p2p.dal.dto.ExperienceRule;
import com.qdlc.p2p.dal.model.ExperienceModel;

/**
 * 体验券管理Aciton
 * @author xxx
 * @version 2.0
 * @date 2015年4月23日 下午5:52:19
 */
public class ManageExperienceAction extends BaseAction<ExperienceModel> implements ModelDriven<ExperienceModel> {

	@Resource
	private ExperienceService experienceService;
	@Resource
	private ExperienceRuleService experienceRuleService;
	
	/** 启用的体验券规则 */
	private List<ExperienceRule> usableExperienceRuleList;

	/**
	 * 体验券实体对象
	 */
	private Experience experience;
	
	private Map<String, Object> data;
	
	@Action("/modules/additional/experience/manage")
	public String manage(){
		request.setAttribute("usableExperienceRuleList", experienceRuleService.findUsableExperienceRule());
		return "manage";
	}
	
	/**
	 * 获取列表信息
	 * @throws IOException
	 */
	@Action("/modules/additional/experience/list")
	public void list() throws Exception{
		data = new HashMap<String, Object>();
		if(StringUtil.isBlank(paramString("status"))){
			model.setStatus((byte)99);
		}
		PageDataList<ExperienceModel> pagaDataList = experienceService.findByModel(model);
		int total = pagaDataList.getPage().getTotal();
		data.put("total", total);
		data.put("rows", pagaDataList.getList());
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 导出Excel_获取列表信息
	 * @throws Exception 
	 */
	@Action("/modules/additional/experience/exportExcelList")
	public void exportExcelList() throws Exception{
		model.setPage(1);
		model.setRows(5000);
		// 初始化status值为99
		if (StringUtil.isBlank(paramString("status"))){
			model.setStatus((byte) 99);
		}
		PageDataList<ExperienceModel> pageDataList = experienceService.findByModel(model);
		
		String title = "用户体验券Excel表";
		
		String[] hearders = new String[] { "编号", "用户名", "真实姓名", "规则", "体验券金额(元)", "获得时间", "使用时间","有效期（天）", "过期时间", "状态" };// 表头数组
		String[] fields = new String[] { "id", "userName", "realName","ruleName", "amount", "addTime", "useTime", "validTime","expiredTime", "statusTip"};// 对象属性数组
		TableData td = ExcelUtils.createTableData(pageDataList.getList(), ExcelUtils.createTableHeader(hearders), fields);
		JsGridReportBase report = new JsGridReportBase(request, response);
		report.exportToExcel(title, getOperator().getUserName(), td);
	}

	/**
	 * 添加
	 * @return
	 */
	@Action("/modules/additional/experience/append")
	public String append(){
		request.setAttribute("usableExperienceRuleList", experienceRuleService.findUsableExperienceRule());
		return "append";
	}
	
	/**
	 * 添加体验券
	 * @throws Exception
	 */
	@Action("/modules/additional/experience/doAppend")
	public void doAppend() throws Exception {
		data = new HashMap<String, Object>();
		//model.validModelData();
		Experience experience = model.prototypeForAppend();
		experience.setSourceType(EnumActicityPlanNid.SYSTEM.getValue());
		experienceService.save(experience);
		data.put("result", "true");
		data.put("msg", MessageUtil.getMessage("MS102001001"));
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 修改
	 * @return
	 */
	@Action("/modules/additional/experience/modify")
	public String modify() {
		request.setAttribute("usableExperienceRuleList", experienceRuleService.findUsableExperienceRule());
		Experience experience = experienceService.findById(model.getId());
		request.setAttribute("experience", experience);
		return "modify";
	}
	
	/**
	 * 修改
	 * @return
	 */
	@Action("/modules/additional/experience/doModify")
	public void doModify() throws Exception {
		data = new HashMap<String, Object>();
		Experience experience = model.prototypeForModify();
		experienceService.update(experience);
		data.put("result", "true");
		data.put("msg", MessageUtil.getMessage("MS102002001"));
		printJson(getStringOfJpaObj(data));
	}

	/**
	 * 禁用
	 * @return
	 */
	@Action("/modules/additional/experience/disable")
	public void disable() throws Exception {
		int resule = experienceService.updateStatus(model.getId(), ExperienceModel.EXPERIENCE_STATUS_INVALID, ExperienceModel.EXPERIENCE_STATUS_UNUSED);
		if(resule == 1){
			printWebSuccess();
		} else {
			printWebResult(MessageUtil.getMessage("MF102003001"), false);
		}
	}
	
	/**
	 * 启用
	 * @return
	 */
	@Action("/modules/additional/experience/enable")
	public void enable() throws Exception {
		int resule = experienceService.updateStatus(model.getId(), ExperienceModel.EXPERIENCE_STATUS_UNUSED, ExperienceModel.EXPERIENCE_STATUS_INVALID);
		if(resule == 1){
			printWebSuccess();
		} else {
			printWebResult(MessageUtil.getMessage("MF102003001"), false);
		}
	}
	

	public List<ExperienceRule> getUsableExperienceRuleList() {
		return usableExperienceRuleList;
	}

	public void setUsableExperienceRuleList(
			List<ExperienceRule> usableExperienceRuleList) {
		this.usableExperienceRuleList = usableExperienceRuleList;
	}

	public Experience getExperience() {
		return experience;
	}

	public void setExperience(Experience experience) {
		this.experience = experience;
	}

	public Map<String, Object> getData() {
		return data;
	}

	public void setData(Map<String, Object> data) {
		this.data = data;
	}

}
